package com.ruoyi.apiserver.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.ruoyi.apiserver.utils.JwtUtils;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.WxLoginBody;
import com.ruoyi.common.core.domain.model.WxPhoneInfo;
import com.ruoyi.common.core.domain.model.weixinPhoneInfo;
import com.ruoyi.common.weixin.weixinHttpUtils;
import com.ruoyi.members.domain.MhUser;
import com.ruoyi.members.service.IMhUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "登录接口接口")
@RequestMapping("/api/login")
public class LoginController {

    @Value("${wx-app.appId}")
    private String appId;

    @Value("${wx-app.appSecret}")
    private String appSecret;


    @Autowired
    private IMhUserService userService;

    @Autowired
    private JwtUtils jwtUtils;

    @Autowired
    private weixinHttpUtils wxHttpUtils;

    /**
     * 小程序一键登录/注册
     */
    @ApiOperation("小程序一键登录/注册")
    @Anonymous
    @PostMapping("/miniapp")
    public AjaxResult miniAppLogin(@RequestBody WxLoginBody loginBody) {
        try{
            // 检查用户是否已存在
            loginBody.setAppId(appId);
            loginBody.setAppSecret(appSecret);

            //解析出来openid和手机号码
            WxPhoneInfo phoneinfo = wxHttpUtils.getPhoneInfo(loginBody);

            if (phoneinfo == null) {
                return AjaxResult.error("登录失败，获取手机号码失败！");
            }
            loginBody.setPhone(phoneinfo.getPhoneNumber());

            MhUser loginUser = userService.miniAppLogin(loginBody);
            loginUser.setPassword("love_you");
            if (loginUser == null) {
                return AjaxResult.error("微信登录失败！");
            }


            // 生成token
            String token = jwtUtils.generateToken(loginUser.getId().toString());

            Map<String, Object> result = new HashMap<>();
            result.put("token", token);
            result.put("user", loginUser);

            return AjaxResult.success("登录成功", result);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    @ApiOperation("获取微信openId")
    @Anonymous
    @PostMapping("/getwxopen")
    public AjaxResult getOpenInfo(@RequestBody WxLoginBody loginBody) throws JsonProcessingException {
       try {
           // 检查用户是否已存在
           loginBody.setAppId(appId);
           loginBody.setAppSecret(appSecret);

           //解析出来openid和手机号码
           Object obj = wxHttpUtils.GetWeixinopenInfo(loginBody);

           return AjaxResult.success("获取成功", obj);
       }catch (Exception e){
           return AjaxResult.error(e.getMessage());
       }
    }




}
